假設 有一隻程式的功能是需要用excel要把受訓成績匯入檔案到db
而他的欄位如下
受訓日期,受訓人員,受訓成績
但是為了要避免重覆輸入,所以我們要把 受訓日期,受訓人員 有重覆的挑出來
重覆的欄位 有兩個 要如何挑出來呢
我的想法是 把 日期+姓名 做group by
這樣就可以很簡單的把 有重覆日期+人員 的資料挑出來了
範例code如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }  
        class employee
        {
            private string m_date;
            private string m_name;
            private int m_score;
            public employee(string cdate, string name, int score)
            {
                m_date = cdate;
                m_name = name;
                m_score = score;
            }
            public override string ToString()
            {
                return  m_date +","+ m_name;
            } 
        }
        private void button1_Click(object sender, EventArgs e)
        {
            List<employee> employees = new List<employee>
            {
                new employee("2012/1/18","龍龍一",100),
                new employee("2012/1/18","龍龍二",100),
                new employee("2012/1/18","龍龍三",100),
                new employee("2012/1/19","龍龍一",100),
                new employee("2012/1/19","龍龍二",100),
                new employee("2012/1/19","龍龍三",100), 
                new employee("2012/1/18","龍龍三",100),
                new employee("2012/1/18","龍龍一",100),
            };
            var q =
from p in employees
group p by p.ToString() into g
where g.Count() > 1 //只顯示超過一次以上的
select new
{
    g.Key,       
    count = g.Count() 
};
            string stremp = "";
            foreach (var x in q)
            {
                stremp += x.ToString() + Environment.NewLine;
            }
            MessageBox.Show(stremp);
        }
    }
}